﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Collections.Generic;

namespace YXShop.Web.filehandle
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class userregform : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            YXShop.BLL.Member.MemberAccount bll = new YXShop.BLL.Member.MemberAccount();
            YXShop.Model.Member.MemberAccount model = new YXShop.Model.Member.MemberAccount();
            //是否允许注册新用户
            if (!sp.IsRegistered)
            {
                ChangeHope.WebPage.Script.AlertAndGoBack("温馨提示：目前不允许新用户注册！");
                
            }

            #region 验证是否已经存在用户ajax
            if (ChangeHope.WebPage.PageRequest.GetFormString("Option") == "Main")
            {

                context.Response.Write(this.UserVa(ChangeHope.WebPage.PageRequest.GetFormString("UserName")));
                context.Response.End();
                return;
            }
            #endregion

            //是否允许重复邮箱注册
            string email=ChangeHope.WebPage.PageRequest.GetFormString("Email");
            if (sp.SameEmailRegister == false && bll.ExistEmail(email))
            {
                ChangeHope.WebPage.Script.AlertAndGoBack("温馨提示：不允许重复的邮箱注册！");
            }
            //是否需要管理员验证
            model.State = sp.AdminValidate == true ? 1 : 0;
            model.Coupons = 0;
            if (DateTime.Now > sp.HandselCouponsBeginTime && DateTime.Now < sp.HandselCouponsEndTime)
            {
                model.Coupons = sp.HandselCoupons == true ? sp.HandselCouponsNumber : 0;
            }

            model.Points = Convert.ToInt32(sp.HandselPoint);
            model.UserGroup = Convert.ToInt32(sp.UserDefaultGroup);
            //服务器端判断是否有相同账号
            if (bll.Exists(ChangeHope.WebPage.PageRequest.GetFormString("regusername")))
            {
                ChangeHope.WebPage.Script.AlertAndGoBack("温馨提示：有重复的用户！");
            }
            if (ChangeHope.WebPage.PageRequest.GetFormString("reguserpwd") == "")
            {
                ChangeHope.WebPage.Script.AlertAndGoBack("温馨提示：密码不能为空！");
            }
            model.UserType = 0;
            model.UserId = ChangeHope.WebPage.PageRequest.GetFormString("regusername");
            model.PassWord = ChangeHope.WebPage.PageRequest.GetFormString("reguserpwd");
            model.Signed = "您的签名";
            model.Question = ChangeHope.WebPage.PageRequest.GetFormString("hfQuestion");
            model.Answer = ChangeHope.WebPage.PageRequest.GetFormString("insolution");
            model.Email = email;        
            model.RegisterDate = DateTime.Now;
            model.RegisterIP = context.Request.UserHostAddress;
            model.LastLoginDate = DateTime.Now;
            model.LastLoginIP = context.Request.UserHostAddress;
            model.LoginTimes = 0;
            model.Capital = 0;
            model.PeriodOfValidity = DateTime.Now.AddMonths(3);
            //验证码
            string vilCode = ChangeHope.WebPage.PageRequest.GetFormString("txtCode").ToLower();
            //如果存在验证码标签
            if (context.Request.Form["txtCode"] != null)
            {
                if (vilCode == "")
                {
                    ChangeHope.WebPage.Script.AlertAndGoBack("温馨提示：请输入验证码");
                }
                else if (!HttpContext.Current.Request.Cookies["CheckCode"].Value.ToLower().Equals(vilCode))
                {
                    ChangeHope.WebPage.Script.AlertAndGoBack("温馨提示：验证码错误");
                }
            }
            if (bll.Add(model) != 0)
            {

                #region 添加积分记录
                YXShop.Model.Member.Integral modelInte = new YXShop.Model.Member.Integral();
                YXShop.BLL.Member.Integral bllInte = new YXShop.BLL.Member.Integral();
                modelInte.Userid = GetIdByUserId(model.UserId);
                modelInte.IntegralClass = 3;
                modelInte.Origin = "注册赠送";
                modelInte.IntegralNum = model.Points;
                modelInte.GainDate = DateTime.Now;
                modelInte.NoteDate = DateTime.Now;
                modelInte.NoteName = "系统自动记录";
                modelInte.Remark = "注册用户时赠送的积分";
                modelInte.IntegralStatus = 0;
                try
                {
                    bllInte.Add(modelInte);
                }
                catch
                {
                    ChangeHope.WebPage.Script.AlertAndGoBack("注册失败！");
                }
                #endregion

                #region 添加点卷记录
                //点卷
                YXShop.Model.Member.UserInfoNote modelNote = new YXShop.Model.Member.UserInfoNote();
                YXShop.BLL.Member.UserInfoNote bllNote = new YXShop.BLL.Member.UserInfoNote();
                modelNote.UserID = GetIdByUserId(model.UserId);
                modelNote.TicketCount = 0;
                modelNote.Causation = "注册赠送的点卷";
                modelNote.BosomNote = "注册赠送的点卷";
                modelNote.NoteDate = DateTime.Parse(DateTime.Now.ToShortDateString());
                modelNote.NoteName = "系统自动记录";
                modelNote.NoteType = 0;
                modelNote.BuckleOrAdd = 0;
                modelNote.Username = model.UserId;
                try
                {
                    bllNote.Add(modelNote);
                }
                catch
                {
                    ChangeHope.WebPage.Script.AlertAndGoBack("注册失败！");
                }
                #endregion

                #region 添加附属信息
                YXShop.Model.Member.MemberInfo modelInfo = new YXShop.Model.Member.MemberInfo();
                YXShop.BLL.Member.MemberInfo bllInfo = new YXShop.BLL.Member.MemberInfo();
                //默认为公开资料
                modelInfo.PrivacySettings = 1;
                modelInfo.TrueName = ChangeHope.WebPage.PageRequest.GetFormString("txtRealName");
                modelInfo.Title = string.Empty;
                modelInfo.Photo = string.Empty;
                //默认生日为？ 
                modelInfo.Birthday = DateTime.Now;
                modelInfo.PapersType = string.Empty;
                modelInfo.PapersNumber = string.Empty;
                modelInfo.Origin = string.Empty;
                modelInfo.Nation = string.Empty;
                modelInfo.Sex = 1;
                modelInfo.Marriage = 0;
                modelInfo.Education = string.Empty;
                modelInfo.GraduateSchool = string.Empty;
                modelInfo.Province = string.Empty;
                modelInfo.City = string.Empty;
                modelInfo.Borough = string.Empty;
                modelInfo.Address = ChangeHope.WebPage.PageRequest.GetFormString("txtStreet");
                modelInfo.Zip = ChangeHope.WebPage.PageRequest.GetFormString("txtZip");
                modelInfo.OfficePhone = string.Empty;
                modelInfo.HomePhone = ChangeHope.WebPage.PageRequest.GetFormString("txtTel");
                modelInfo.MobilePhone = ChangeHope.WebPage.PageRequest.GetFormString("txtMobile");
                modelInfo.HandPhone = string.Empty;
                modelInfo.Fax = ChangeHope.WebPage.PageRequest.GetFormString("txtFax");
                modelInfo.PersonWebSite = string.Empty;
                modelInfo.QQ = string.Empty;
                modelInfo.MSN = ChangeHope.WebPage.PageRequest.GetFormString("txtMsn");
                modelInfo.ICQ = string.Empty;
                modelInfo.UC = string.Empty;
                modelInfo.LifeHobbies = string.Empty;
                modelInfo.CultureHobbies = string.Empty;
                modelInfo.Entertainment = string.Empty;
                modelInfo.SportsHobbies = string.Empty;
                modelInfo.OtherHobbies = string.Empty;
                //公司名称 
                modelInfo.IncName = ChangeHope.WebPage.PageRequest.GetFormString("txtCname");
                modelInfo.Department = string.Empty;
                modelInfo.Positions = ChangeHope.WebPage.PageRequest.GetFormString("txtPost");
                modelInfo.WorkRange = string.Empty;
                //公司地址
                modelInfo.IncAddress = ChangeHope.WebPage.PageRequest.GetFormString("txtAddress");
                modelInfo.MonthlyInCome = string.Empty;
                //得到自增ID
                modelInfo.UID = GetIdByUserId(model.UserId);
                try
                {
                    bllInfo.Add(modelInfo);
                }
                catch
                {
                    ChangeHope.WebPage.Script.AlertAndGoBack("注册失败！");
                }
                #endregion
                string url = YXShop.TemplateAction.Common.UrlStr("login", "", "membercenter/");
                ChangeHope.WebPage.Script.AlertAndRedirect("注册成功", url);
            }
            else
            {
                ChangeHope.WebPage.Script.AlertAndGoBack("注册失败！");
            }

        }

        #region 验证用户是否存在
        protected  string UserVa(string UserName)
        {
            string RegStr = "";
            YXShop.BLL.Member.MemberAccount bll = new YXShop.BLL.Member.MemberAccount();
            string parm = ChangeHope.Common.StringHelper.InputTexts(UserName);
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            if (sp.ForbidUserId != "")
            {
                string[] strForbid = sp.ForbidUserId.Split(',');
                foreach (string str in strForbid)
                {
                    if (parm.IndexOf(str)>-1)
                    {
                        RegStr = "您设置的用户名为非法用户名！";
                        break;
                    }
                }
            }

            if (bll.Exists(parm))
            {
                RegStr = "您设置的用户名已存在，请另输入用户名！";
            }
            return RegStr;
        }

        /// <summary>
        /// 根据账号得到自增ID
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        protected int GetIdByUserId(string UserId)
        {
            YXShop.BLL.Member.MemberAccount bll = new YXShop.BLL.Member.MemberAccount();
            YXShop.Model.Member.MemberAccount model = bll.GetModel(UserId);
            if (model != null)
            {
                return model.UID;
            }
            else
            {
                return -1;
            }
        }
        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
